package com.teng.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.teng.ao.QueryUserAo;
import com.teng.dao.UserDao;
import com.teng.dto.QueryResult;
import com.teng.entity.UserEntity;
import com.teng.exception.OAException;
import com.teng.service.UserCrudService;
import com.teng.util.MD5;
import com.teng.util.StringUtil;
import com.teng.util.Uuid;


public class UserCrudServiceImpl implements UserCrudService {
	@Override
	public UserEntity myaccount(String id) throws Exception {
		UserDao ud=new UserDao();
		UserEntity ue=ud.myaccount(id);
		
		
		return ue;
	}
	
	
	@Override
	public void unlock(String id) throws Exception {
		if(id==null||(id=id.trim()).length()==0) {
			throw new OAException("错误");
		}
		UserDao u=new UserDao();
		u.unlock(id);
		
	}
	
	@Override
	public void lock(String id) throws Exception {
		if(id==null||(id=id.trim()).length()==0) {
			throw new OAException("错误");
		}
		UserDao u=new UserDao();
		u.lock(id);
		
	}
	@Override
	public void delete(String id)throws Exception{
		if(id==null||(id=id.trim()).length()==0) {
			throw new OAException("错误");
		}
		UserDao u=new UserDao();
		u.delete(id);
	}
	
	@Override
	public void updata(String id, String account, String name, String remark) throws Exception {
		if(id==null||(id=id.trim()).length()==0) {
			throw new OAException("错误");
		}
		if(name==null||(name=name.trim()).length()==0) {
			throw new OAException("名字不能为空");
		}
		if(account==null||(account=account.trim()).length()==0) {
			throw new OAException("账号不能为空");
		}
		
		if(account.length()<6||account.length()>32) {
			throw new OAException("账号长度必须在6位和32位之间");
		}
		
		UserDao ud=new UserDao();
		boolean b=ud.validateAccount(account);
		if(b==true) {
			throw new OAException("账号已存在");
		}
		
		Pattern pattern = Pattern.compile("\\w+");
		 Matcher matcher = pattern.matcher(name);
		if(matcher.matches()==false) {
			throw new OAException("名字必须是中文");
		}
		Map<String, Object> map=new HashMap<>(3,075f);
		map.put("account", account);
		map.put("name", name);
		map.put("remark", remark);
		ud.update(id, map);
	}
	@Override
	public UserEntity check(String id) throws Exception {
		if(id==null||(id=id.trim()).length()==0) {
			throw new OAException("错误");
		}
		UserDao ud=new UserDao();
		UserEntity u=ud.selectById(id);
		if(u==null) {
			throw new OAException("不存在或者已被删除");
		}
		return u;
	}
	
	@Override
	public QueryResult queryUser(QueryUserAo ao,String creatId) throws Exception {

		String key=StringUtil.blankAsNull(ao.getKey());
		int pageSize=StringUtil.parseInt(ao.getPageSize(),10);
		int pageNo=StringUtil.parseInt(ao.getPageNo(),1);
		Integer state=StringUtil.parseInt(ao.getState(),null);
		
		UserDao udao=new UserDao();
		
		List<UserEntity> rows=new ArrayList<>(pageSize);
		
		int totalRows=udao.selectByLike(rows, key, state, pageNo, pageSize,creatId);
		
		QueryResult result=new QueryResult();
		
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setRows(rows);
		result.setTotalRows(totalRows);
		return result;
	}
	@Override
	public UserEntity insert(String account, String name, String remark,String creatId) throws Exception {
		if(account==null||(account=account.trim()).length()==0) {
			throw new OAException("账号不能为空");
		}
		if(name==null||(name=name.trim()).length()==0) {
			throw new OAException("名字不能为空");
		}
		if(account.length()<6||account.length()>32) {
			throw new OAException("账号长度必须在6位和32位之间");
		}
		UserDao ud=new UserDao();
		boolean b=ud.validateAccount(account);
		if(b==true) {
			throw new OAException("账号已存在");
		}
		Pattern pattern = Pattern.compile("\\w+");
		 Matcher matcher = pattern.matcher(account);
		if(matcher.matches()==false) {
			throw new OAException("账户必须是英文");
		}
		if(pattern.matcher(name).matches()==true) {
			throw new OAException("名称必须是中文");
		}
		
		 UserEntity u=new UserEntity();
		 u.setId(Uuid.uuid());
		 u.setAccount(account);
		 u.setName(name);
		 u.setPassword(MD5.encode("123456"));
		 u.setCreateTime(new Date());
		 u.setLastLoginIP(null);
		 u.setLastLoginTime(null);
		 u.setLastModifyPasswordTime(null);
		 u.setPhone(null);
		 u.setSecurityEmail(null);
		 u.setAvatar("/ff/ff/.png");
		 u.setRemark(remark);
		 u.setFlag(0);
		 u.setDeleted(0);
		 u.setCreateUserId(creatId);
		 
		 ud.insert(u);
		 
		return u;
	}
	

}
